﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using System.IO;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Net;
using WFinger.Web;
using System.Text;
using WFinger.Common;

using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WFinger.CMS;
namespace WFinger.Web.user.info
{
    public partial class onlineReturn : WFinger.Web.UI.ManagePage
    {
        public String Get_Http(String a_strUrl, int timeout)
        {
            string strResult;
            try
            {
                HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(a_strUrl);
                myReq.Timeout = timeout;
                HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
                Stream myStream = HttpWResp.GetResponseStream();
                StreamReader sr = new StreamReader(myStream, Encoding.Default);
                StringBuilder strBuilder = new StringBuilder();
                while (-1 != sr.Peek())
                {
                    strBuilder.Append(sr.ReadLine());
                }

                strResult = strBuilder.ToString();
            }
            catch (Exception exp)
            {

                strResult = "错误：" + exp.Message;
            }

            return strResult;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string alipayNotifyURL = "https://www.alipay.com/cooperate/gateway.do?service=notify_verify";
            //string alipayNotifyURL = "http://notify.alipay.com/trade/notify_query.do?";//此路径是在上面链接地址无法起作用时替换使用。
            string key = "hgfwrde8v7yyibhlrktb35gwgruw14wa"; //partner 的对应交易安全校验码（必须填写）
            string partner = "2088002109809779"; 		//partner合作伙伴id（必须填写）
            string _input_charset = "utf-8";//编码类型，完全根据客户自身的项目的编码格式而定，千万不要填错。否则极其容易造成MD5加密错误。

            alipayNotifyURL = alipayNotifyURL + "&partner=" + partner + "&notify_id=" + Request.QueryString["notify_id"];

            //获取支付宝ATN返回结果，true是正确的订单信息，false 是无效的
            string responseTxt = Get_Http(alipayNotifyURL, 120000);
            int i;
            NameValueCollection coll;
            //Load Form variables into NameValueCollection variable.
            coll = Request.QueryString;

            // Get names of all forms into a string array.
            String[] requestarr = coll.AllKeys;

            //进行排序；
            string[] Sortedstr = AliPay.BubbleSort(requestarr);

            //构造待md5摘要字符串 ；

            StringBuilder prestr = new StringBuilder();

            for (i = 0; i < Sortedstr.Length; i++)
            {
                if (Request.Form[Sortedstr[i]] != "" && Sortedstr[i] != "sign" && Sortedstr[i] != "sign_type")
                {
                    if (i == Sortedstr.Length - 1)
                    {
                        prestr.Append(Sortedstr[i] + "=" + Request.QueryString[Sortedstr[i]]);
                    }
                    else
                    {
                        prestr.Append(Sortedstr[i] + "=" + Request.QueryString[Sortedstr[i]] + "&");

                    }
                }
            }

            prestr.Append(key);

            //生成Md5摘要；
            string mysign = AliPay.GetMD5(prestr.ToString(), _input_charset);

            string sign = Request.QueryString["sign"];

            //  Response.Write(prestr.ToString());  //调试用，支付宝服务器返回时的完整路径。

            if (mysign == sign && responseTxt == "true")   //验证支付发过来的消息，签名是否正确
            {

                //更新自己数据库的订单语句，请自己填写一下
                string strOrderNO = Request.QueryString["out_trade_no"];//订单号
                string strPrice = Request.QueryString["price"];//金额

                PageRight("订单号：" + strOrderNO + "<br>金额：" + strPrice, "logs.aspx");

                // Response.Write();     //成功，可美化该页面，提示信息
            }
            else
            {
                // Response.Write("------------------------------------------");
                // Response.Write("<br>Result:responseTxt=" + responseTxt);
                // Response.Write("<br>Result:mysign=" + mysign);
                // Response.Write("<br>Result:sign=" + sign);
                //Response.Write("");                                             //支付失败，提示信息
                PageRight("支付失败", "main.aspx");
            }
        }
    }
}